
package ysw.jdbc;
import ysw.dao.CarDao;
import ysw.databaseconnection.*;
import java.sql.*;
import ysw.vo.*;
import java.util.ArrayList;
import java.util.List;

public class CarJdbc implements CarDao{
    private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public CarJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }
public Car addCar(Car car){
        String sql="insert into cars values(?,?,?,?,?)";
        try{
            psmt=this.getConn().prepareStatement(sql);
            
 
            psmt.setString(1,car.getCarnumber());
            psmt.setString(2,car.getCartype());
            psmt.setInt(3,car.getCid());
            psmt.setString(4,car.getCname());
            psmt.setByte(5,car.getColor());
          
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return car;
        }


    

}
